package xdj.android.personal.namespace.Views;

import xdj.android.personal.R;
import xdj.android.personal.namespace.Business.UserDBAccess;
import xdj.android.personal.namespace.Entity.UserEntity;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class XdjAndroidActivity extends Activity
{
	UserDBAccess _dbAccess;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		super.setTitle(getResources().getString(R.string.LoginViewTitle));
		//Bind Login Click Event
		_dbAccess = new UserDBAccess(this);
		LoginClick();
	}
	
	void LoginClick()
	{
		final Button login = (Button)findViewById(R.id.btn_login);
		login.setOnClickListener(new OnClickListener(){
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				//
 				EditText etUserName = (EditText)findViewById(R.id.et_UserName);
 				EditText etPassWord = (EditText)findViewById(R.id.et_PassWord);
 				TextView tvMessage = (TextView)findViewById(R.id.tv_NotifacationMessage);
			    Resources r = getResources();
 				UserEntity para = new UserEntity();
 				para.Name = etUserName.getText().toString(); 
 				para.PassWord = etPassWord.getText().toString();
 				if(!_dbAccess.Query(para))
 				{
 					tvMessage.setText(r.getString(R.string.login_ErrorMessage));
 					tvMessage.setTextColor(android.graphics.Color.RED);
 					return;
 				}
 				UserEntity qResult = _dbAccess.GetOneQueryResult();
 				if(qResult.Name ==  null || qResult.Name.isEmpty())
 				{
 					tvMessage.setText(r.getString(R.string.login_ErrorMessage));
 					tvMessage.setTextColor(android.graphics.Color.RED);
 					return;
 				}
 				//
 			
 				Intent intent = new Intent(XdjAndroidActivity.this,XdjAndroidLineActivity.class);
 				startActivity(intent);
 				
 				//
			    tvMessage.setText(r.getString(R.string.Login_NoticeMessage));
 				tvMessage.setTextColor(android.graphics.Color.WHITE);
 				etUserName.setText("");
 				etPassWord.setText("");
 			}
         	
         });
	}
}